package com.axzhengxin.platform.assistant.controller;

import com.alibaba.fastjson.JSONObject;
import com.axzhengxin.platform.afterloan.domain.FspCorInf;
import com.axzhengxin.platform.afterloan.service.IFspCorInfoService;
import com.axzhengxin.platform.assistant.domain.BizMarketInfo;
import com.axzhengxin.platform.assistant.service.IBizMarketInfoService;
import com.axzhengxin.platform.business.domain.FspCorInfBusiness;
import com.axzhengxin.platform.business.service.IBizFlowInfoService;
import com.axzhengxin.platform.common.service.IDbExecService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.jetbrains.annotations.TestOnly;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;
import java.util.Optional;

/**
 * 【请填写功能名称】Controller
 *
 * @author axzhengxin
 * @date 2021-03-05
 */
@Slf4j
@RestController
@RequestMapping("/assistant/bizMarketInfo")
public class BizMarketInfoController extends BaseController {
    @Autowired
    private IBizMarketInfoService bizMarketInfoService;
    @Autowired
    private IDbExecService execService;
    @Autowired
    private IFspCorInfoService fspCorInfoService;


    /**
     * 查询营销名单列表
     */
    @PreAuthorize("@ss.hasPermi('assistant:bizMarketInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizMarketInfo bizMarketInfo) {
        startPage();
        bizMarketInfo.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        List<BizMarketInfo> list = bizMarketInfoService.selectBizMarketInfoList(bizMarketInfo);
        return getDataTable(list);
    }

    /**
     * 下载营销名单模板
     */
    //@PreAuthorize("@ss.hasPermi('assistant:bizMarketInfo:import')")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate(BizMarketInfo bizMarketInfo) {
        ExcelUtil<BizMarketInfo> util = new ExcelUtil<>(BizMarketInfo.class);
        return util.importTemplateExcel("营销名单");
    }


    /**
     * 下载营销名单模板导入
     */
    @Log(title = "营销名单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<BizMarketInfo> util = new ExcelUtil<>(BizMarketInfo.class);
        List<BizMarketInfo> bizMarketInfoList= util.importExcel(file.getInputStream());
        String message = bizMarketInfoService.importBizMarkrtInfo(bizMarketInfoList);
        return AjaxResult.success(message);
    }



    /**
     * 获取营销名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('assistant:bizMarketInfo:query')")
    @GetMapping(value = "/{marketId}")
    public AjaxResult getInfo(@PathVariable("marketId") Long marketId) {
        BizMarketInfo bizMarketInfo = bizMarketInfoService.selectBizMarketInfoLittleById(marketId);
        FspCorInf fspCorInf = fspCorInfoService.getCustomerInfoByName(bizMarketInfo.getCorName());
        fspCorInf.setBizMarketInfo(bizMarketInfo);
        if ( null != fspCorInf){
            execService.getJbxxByEntName(bizMarketInfo.getCorName())
                    .ifPresent(result -> {
                        fspCorInf.setEconat(result.getString("ECONAT"));
                        fspCorInf.setEntState(result.getString("STATE"));
                        fspCorInf.setOpScope(result.getString("OPSCOPE"));
                        fspCorInf.setEntTypeName(result.getString("ENTTYPENAME"));
                        try {
                            fspCorInf.setEstDate(DateUtils.parseDate(result.getString("ESTDATE"), "yyyy-MM-dd HH:mm:ss"));
                            fspCorInf.setApprDate(DateUtils.parseDate(result.getString("APPRDATE"), "yyyy-MM-dd HH:mm:ss"));
                        } catch (ParseException e) {
                            log.error("解析日期字符串发生异常", e);
                        }
                    });
            return AjaxResult.success(fspCorInf);
        }else {
            return AjaxResult.error("此用户数据库中不存在，请进行核对");
        }
    }


}
